package com.sykuku.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.sykuku.common.JacksonObjectMapper;
import com.sykuku.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

@Slf4j
@Configuration//不能配置WebMvcConfigurationSupport 不然拦截动态界面失效
@EnableSwagger2
@EnableKnife4j
public class springMvcConfig implements WebMvcConfigurer {

    /*配置映射静态资源*/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开启静态资源映射");
        /*如果访问路径是addResourceHandler中的filepath这个路径 那么就 映射到访问本地的
        addResourceLocations 的参数的这个路径上*/
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 注入一个拦截器配置
     *
     * @return
     */
    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    ;

    //添加一个自定义的拦截配置,按照拦截属性值设置拦截范围addpathpatterns
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("拦截器配置成功");
        registry.addInterceptor(loginInterceptor())
                .addPathPatterns("/**")//放行的是静态资源界面 拦截的是进入界面后发送的异步请求动态请求
                .excludePathPatterns("/backend/**",
                        "/front/**",
                        "/employee/login",
                        "/common/**",
                        "/user/sendMsg",
                        "/user/login"
                );//放行的是路径下的资源和请求
    }

    /**
     * 扩展mvc框架的消息转换器
     *
     * @param converters
     */
    //用自定义的消息转换器
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("扩展消息转换器启动");
        //创建消息转换器对象 映射 Jackson 2 Http 消息转换器
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器，底层使用Jackson将Java对象转为json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //将上面的消息转换器对象追加到mvc框架的转换器集合中
        converters.add(0, messageConverter);
    }

    /**
     * 在配置类中声明一个Docket类型的bean, 通过该bean来指定生成文档的信息
     * @return
     */
    @Bean
    public Docket createRestApi() {
        // 文档类型
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sykuku.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("瑞吉外卖")
                .version("1.0")
                .description("瑞吉外卖接口文档")
                .build();
    }
}

